<?php namespace App\Models\Phm\Phmtask;

use App\Models\Phm\ModelPHM;
use CodeIgniter\Database\ConnectionInterface;
use CodeIgniter\Validation\ValidationInterface;

class ModelPhmtask extends ModelPHM
{
    function __construct(ConnectionInterface $db = null, ValidationInterface $validation = null)
    {
        parent::__construct($db, $validation);
    }

    public function getPhmtaskById($AID)
    {
        //sql语句
        $sql = "SELECT 
                    `ID`,
                    `PKID`, 
                    `TITLE`, 
                    `ACNO` ,
                    `ACTYPE`, 
                    `ATA`,
                    `TEXT`,
                    `DATE` ,
                    `STATUE` ,
                    `CREATEPEOPLE`,
                    `MODPEOPLE` ,
                    `CREATETIME`,
                    `MODTIME` 
               FROM `PHM_PHMTASK` where (   (`ID`  =  ?) AND ( `DELETED` = '')  )  ;";
        //带参数条件查询
        $rst = $this->db->query($sql,
            [$AID]
        );
        if ($rst->getNumRows() == 1) {
            return $rst->getResultArray()[0];
        } else {
            exit();
        }
    }

    public function getPhmtaskPkidById($AID)
    {
        //sql语句
        $sql = "SELECT `PKID` FROM `PHM_PHMTASK` where (
                                                            (? = `ID`) 
                                                       )  
                                     ;";
        //查看是否数量为1
        return $this->db->query($sql,
            [$AID]
        )->getRow()->PKID;
    }

    public function basedatMod($APara)
    {
        $Arr = array(
            'TITLE' => $APara['TITLE'],
            'DATE' => $APara['DATE'],
            'TEXT' => $APara['TEXT'],
            'ACNO' => $APara['ACNO'],
            'ACTYPE' => $APara['ACTYPE'],
            'ATA' => $APara['ATA'],
            'STATUE' => $APara['STATUE'],
            'CREATEPEOPLE' => '吴宁',
            'MODPEOPLE' => '吴宁',
        );
        return $this->db->table('PHM_PHMTASK')->update($Arr, array('id' => $APara['ID']));
    }

    public function basedatDeleteByID($AID)
    {
        $Arr = array(
            'DELETED' => 1,
            'COMMENT' => F_UnixToFullString(time()) . '删除',
        );
        $this->db->table('PHM_PHMTASK')->update($Arr, array('id' => $AID));
        return $this->db->affectedRows();
    }

    public function phmtaskAdd($APara)
    {
        $Arr = array(
            'PKID' => '',
            'TITLE' => $APara['TITLE'],
            'DATE' => $APara['DATE'],
            'TEXT' => $APara['TEXT'],
            'ACNO' => $APara['ACNO'],
            'ACTYPE' => $APara['ACTYPE'],
            'ATA' => $APara['ATA'],
            'STATUE' => $APara['STATUE'],
            'CREATEPEOPLE' => '吴宁',
            'MODPEOPLE' => '吴宁',
            'CREATETIME' => time(),
            'MODTIME' => time(),
            'DELETED' => '',
            'COMMENT' => '',
        );
        //插入
        $m_OK = $this->db->table('PHM_PHMTASK')->insert($Arr);
        //插入成功，更新PKID
        if ($m_OK) {
            $ID = $this->db->insertID();
            $data = array(
                'PKID' => date('YmdHis', time()) . sprintf('%06d', $ID)
            );
            $this->db->table('PHM_PHMTASK')->update($data, array('id' => $ID));
            if ($this->db->affectedRows() > 0) {
                return $ID;
            } else {
                return '';
            }
        } else {
            return '';
        }
    }

    //查询返回array形式
    public function getPhmtaskCount($AQueryData)
    {
        //查询条件
        $keyWord1 = FormatEmpty($AQueryData['KeyWord1'], '%');

        $sql = "SELECT COUNT(1) NUM FROM `PHM_PHMTASK` A WHERE 
          (
                (                                
                    ((? = '') OR (`TITLE` LIKE ?)  OR ( `TEXT` LIKE ? )  )
                ) AND   ( `DELETED` = '')  
          ) ;";
        //查看是否数量为1
        return $this->db->query($sql,
            [$keyWord1, $keyWord1, $keyWord1]
        )->getRow()->NUM;
    }

    public function getPhmtask($AQueryData)
    {
        //ShowMessage($AQueryData);
        //查询条件
        $keyWord1 = FormatEmpty($AQueryData['KeyWord1'], '%');
        $sql = "SELECT 
                    `ID`,
                    `PKID`, 
                    `TITLE`, 
                    `ACNO` ,
                    `ACTYPE`, 
                    `ATA`,
                    `TEXT`,
                    `DATE` ,
                    `STATUE` ,
                    `CREATEPEOPLE`,
                    `MODPEOPLE` ,
                    `CREATETIME`,
                    `MODTIME`  
                    FROM `PHM_PHMTASK` A WHERE 
          (
                (                                
                    ((? = '') OR (`TITLE` LIKE ?)  OR ( `TEXT` LIKE ? )  )
                ) AND   ( `DELETED` = '')  
          )  ORDER BY `ID` DESC {$AQueryData['limit']}  ;";
        //带参数条件查询
        return $this->db->query($sql,
            [$keyWord1, $keyWord1, $keyWord1]
        )->getResultArray();
    }
}